package org.chnxi.study.nio.netty.groupchat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

public class NioGroupChatServerHeartbeatHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        if(evt instanceof IdleStateEvent){
            IdleStateEvent event = (IdleStateEvent)evt;
            switch (event.state()){
                case READER_IDLE:
                    System.out.println("-----检测到读空闲----->"+ctx.channel().remoteAddress());
                    break;
                case WRITER_IDLE:
                    System.out.println("-----检测到写空闲----->"+ctx.channel().remoteAddress());
                    break;
                case ALL_IDLE:
                    System.out.println("-----检测到读写空闲----->"+ctx.channel().remoteAddress());
                    break;
            }
            //可以针对空闲状态进行业务处理，如关闭无用的通道释放服务器资源
            //ctx.channel().close()
        }

    }
}
